import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import { getToken } from '../utils/auth'
import NProgress from 'nprogress' // progress bar
const _import = require('./_import_' + process.env.NODE_ENV)
// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading

Vue.use(Router)

/* Layout */
import Layout from '../views/layout/XLayout'

/**
* hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
* alwaysShow: true               if set true, will always show the root menu, whatever its child routes length
*                                if not set alwaysShow, only more than one route under the children
*                                it will becomes nested mode, otherwise not show the root menu
* redirect: noredirect           if `redirect:noredirect` will no redirct in the breadcrumb
* name:'router-name'             the name is used by <keep-alive> (must set!!!)
* meta : {
    roles: ['admin','editor']     will control the page roles (you can set multiple roles)
    title: 'title'               the name show in submenu and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar,
    noCache: true                if false ,the page will no be cached(default is false)
  }
**/
export const constantRouterMap = [
  { path: '/login',
    component: _import('login/index'),
    beforeEnter: (to, from, next) => {
      if (getToken()) {
        next({ path: '/' })
      } else {
        next()
      }
      NProgress.done()
    },
    hidden: true
  },
  { path: '/signup', component: _import('signup/index'), hidden: true },
  { path: '/retrieve', component: _import('retrieve/index'), hidden: true },
  { path: '/starting',
    component: { template: `<router-view></router-view>` },
    beforeEnter: (to, from, next) => {
      if (store.getters.team !== '') {
        next({ path: '/' })
      } else {
        next()
      }
      NProgress.done()
    },
    hidden: true,
    children: [
      { path: 'student',
        redirect: '/starting/student/first',
        component: { template: `<router-view></router-view>` },
        name: 'starting-student',
        hidden: true,
        children: [
          {
            path: 'first',
            component: _import('starting/student/first'),
            name: 'starting-student-first',
            hidden: true
          }, {
            path: 'second',
            component: _import('starting/student/second'),
            name: 'starting-student-second',
            hidden: true
          }
        ]
      },
      { path: 'admin', component: _import('starting/admin/index'), name: 'starting-admin', hidden: true },
      { path: '', component: _import('starting/index'), name: 'starting', hidden: true }
    ]
  },
  { path: '/404', component: _import('errorPage/404'), hidden: true },
  { path: '/401', component: _import('errorPage/401'), hidden: true },
  {
    path: '',
    component: Layout,
    redirect: 'dashboard',
    children: [{
      path: 'dashboard',
      component: _import('dashboard/index'),
      name: 'dashboard',
      meta: { title: 'dashboard', icon: 'dashboard', noCache: true }
    }]
  }
]

/** note: submenu only apppear when children.length>=1
 *   detail see  https://panjiachen.github.io/vue-element-admin-site/#/router-and-nav?id=sidebar
 **/

export const asyncRouterMap = [
  {
    path: '/announcement',
    name: 'announcement',
    redirect: '/announcement/index',
    meta: { title: 'announcement', icon: 'snooze' },
    component: Layout,
    children: [
      { path: 'index', component: _import('announcement/index'), name: 'announcement-index', meta: { title: 'announcementList', icon: 'snooze' }},
      { path: 'detail/:announcementId', component: _import('announcement/detail'), name: 'announcement-detail', hidden: true, meta: { title: 'announcementDetail', noCache: true }},
      { path: 'tags', component: _import('announcement/tags'), name: 'announcement-tags', meta: { title: 'announcementTags', roles: ['admin', 'announcement'] }},
      { path: 'edit', component: _import('announcement/edit'), name: 'announcement-edit', meta: { title: 'announcementEdit', roles: ['admin', 'announcement'] }}
    ]
  },

  {
    path: '/resource',
    redirect: '/resource/index',
    component: Layout,
    children: [{
      path: 'index',
      component: _import('resource/index'),
      name: 'resource',
      meta: { title: 'resource', icon: 'attach_file' }
    }]
  },

  {
    path: '/vote',
    redirect: '/vote/index',
    component: Layout,
    children: [{
      path: 'index',
      component: _import('vote/index'),
      name: 'vote',
      meta: { title: 'vote', icon: 'flag', noCache: true }
    }, {
      path: 'detail/:voteId',
      component: _import('vote/detail'),
      name: 'vote-detail',
      hidden: true,
      meta: { title: 'voteDetail' }
    }, {
      path: 'buckets/:voteId',
      component: _import('vote/buckets'),
      name: 'vote-buckets',
      hidden: true,
      meta: { title: 'voteBuckets', roles: ['admin', 'vote'] }
    }]
  },

  {
    path: '/registry',
    name: 'registry',
    redirect: '/registry/index',
    component: Layout,
    meta: { title: 'registry', icon: 'content_copy' },
    children: [{
      path: 'index',
      component: _import('registry/index'),
      name: 'registry-index',
      meta: { title: 'registryList', icon: 'content_copy', noCache: true }
    }, {
      path: 'edit',
      component: _import('registry/edit'),
      name: 'registry-edit',
      meta: { title: 'registryEdit', roles: ['admin', 'vote'] }
    }, {
      path: 'detail',
      component: _import('registry/detail'),
      name: 'registry-detail',
      hidden: true,
      meta: { title: 'registryDetail' }
    }]
  },

  {
    path: '/affair',
    redirect: '/affair/index',
    component: Layout,
    children: [{
      path: 'index',
      component: _import('affair/index'),
      name: 'affair',
      meta: { title: 'affair', icon: 'work', noCache: true }
    }]
  },

  {
    path: '/discussion',
    redirect: '/discussion/index',
    component: Layout,
    children: [{
      path: 'index',
      component: _import('discussion/index'),
      name: 'discussion',
      meta: { title: 'discussion', icon: 'question_answer', noCache: true }
    }]
  },

  {
    path: '/contrast',
    redirect: '/contrast/index',
    component: Layout,
    children: [{
      path: 'index',
      component: _import('contrast/index'),
      name: 'contrast',
      meta: { title: 'contrast', icon: 'compare', noCache: true }
    }]
  },

  {
    path: '/management',
    redirect: '/management/index',
    component: Layout,
    meta: { title: 'management', icon: 'filter_vintage', roles: ['admin'] },
    children: [
      { path: 'index', component: _import('management/index'), name: 'management', meta: { title: 'adminOverview' }},
      { path: 'members', component: _import('management/members'), name: 'management-members', meta: { title: 'adminMembers' }},
      { path: 'tags', component: _import('management/tags'), name: 'management-tags', meta: { title: 'adminTags' }}
    ]
  },

  { path: '*', redirect: '/404', hidden: true }
]

export default new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})
